package org.yds.quality.model;

import com.googlecode.aviator.AviatorEvaluator;

import java.util.HashMap;
import java.util.Map;

public class RuleEngine {

    public void executeRule(RuleModel ruleModel, ExecutionContext executionContext) {

        Map<String, Object> env = executionContext.getInput();
        // 执行表达式
        boolean isValid = (boolean) AviatorEvaluator.execute(ruleModel.getExpression(),env);

        // 如果表达式满足，则触发事件
        if (isValid) {
            // 获取事件所需的参数
            Map<String, Object> eventParams = new HashMap<>();
            for (String param : ruleModel.getEventParams()) {
                eventParams.put(param, executionContext.getInput().get(param));
            }
            // 触发事件，并将事件参数传递给外部系统
            EventTrigger.triggerEvent(ruleModel.getEventType(), eventParams);
        }
    }
}